package com.annoncesautomobile.application;

import java.io.Serializable;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.annoncesautomobile.modele.util.ModelXML;

@ReportsCrashes(formKey = "", // will not be used
customReportContent = {ReportField.AVAILABLE_MEM_SIZE,ReportField.SHARED_PREFERENCES, ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT })
public class AnnonceAutomobile extends Application {

	private Thread.UncaughtExceptionHandler androidDefaultUEH;
	
	private static ModelXML staticInstance;
	
	private ModelXML instance;
	
	@Override
	public void onCreate() {
		super.onCreate();
		this.instance = ModelXML.getInstance();
		staticInstance = ModelXML.getInstance();
		ACRA.init(this);
		this.androidDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(handler);
		MailService ms = new MailService();
		ACRA.getErrorReporter().setReportSender(ms);
	}
		
	private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
		public void uncaughtException(Thread thread, final Throwable ex) {
			Log.e("AnnonceAutomobile", "Uncaught exception is: ", ex);
			System.out.println("ICI 1");
			//Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
			Intent i = new Intent(AnnonceAutomobile.this, ErrorHandlerActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			System.out.println("ICI 2");
			Bundle extras = new Bundle();
			extras.putSerializable("exception", (Serializable) ex);
			i.putExtras(extras);
			System.out.println("ICI 3");
			try
			{
				startActivity(i);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			System.out.println("Je kill AnnonceAuto");
			
			android.os.Process.killProcess(android.os.Process.myPid());
			androidDefaultUEH.uncaughtException(thread, ex);
			System.exit(0);
			
			
			/*
			
			progressDialog.show();
			ACRA.getErrorReporter().handleException(ex);
			progressDialog.dismiss();
			Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			android.os.Process.killProcess(android.os.Process.myPid());
			androidDefaultUEH.uncaughtException(thread, ex);
			System.exit(0);
			*/
		}
	};
	
	
}
